/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-04-08
 * Time:10:58
 */

class ListNode {
    public int value;
    public ListNode next;

    public ListNode(int value) {
        this.value = value;
    }
}

public class MyLinkedList {
    public ListNode head;

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
            return;
        }
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if (this.head == null) {
            this.head = node;
            return;
        }
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //打印链表
    public void print() {
        ListNode cur = this.head;

        while (cur != null) {
            System.out.println(cur.value);
            cur = cur.next;
        }
    }

    //返回链表的长度
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index < 0 || index > this.size()) {
            System.out.println("坐标不合法");
            return;
        }
        if (index == 0) {
            this.addFirst(data);
            return;
        }
        if (index == this.size()) {
            this.addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        index--;
        while (index != 0) {
            index--;
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        if (this.head.value == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.value != key) {
                cur = cur.next;
            } else {
                break;
            }
        }
        if (cur.next == null) {
            System.out.println("没有该节点");
            return;
        }
        cur.next = cur.next.next;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        //头节点不能放在开始处理，因为处理了头结点第二个节点就相当新的头节点
        ListNode cur = head;
        ListNode prev = head;
        while (cur != null) {
            if (cur.value == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        //处理头
        if (this.head.value == key) {
            this.head = this.head.next;
        }
    }

    public void clear() {
        this.head = null;
    }

    //逆序
    public void reverseList() {
        ListNode cur = this.head;
        ListNode prev = null;
        ListNode curNext = cur.next;
        while (cur != null) {
            curNext = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curNext;

        }
        this.head = prev;
    }

    //返回中间节点
    public ListNode MidNode() {
        ListNode slow = this.head;
        ListNode fast = this.head;
        while (fast != null && fast.next != null) {
            //fast走两步
            fast = fast.next;
            fast = fast.next;
            //slow走一步
            slow = slow.next;
        }
        return slow;
    }

    //遍历链表一遍，找到倒数k位置的下标
    public ListNode SearchK(int k) {
        if (k < 0 || k > this.size()) {
            System.out.println("k坐标非法");
            return null;
        }
        ListNode fast = this.head;
        ListNode slow = this.head;

        for (int i = 0; i < k - 1; i++) {
            fast = fast.next;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    //合并链表
    public ListNode merge(ListNode head1, ListNode head2) {
        ListNode newHead = new ListNode(0);
        ListNode tmp = newHead;
        while (head1 != null && head2 != null) {
            if (head1.value < head2.value) {
                tmp.next = head1;
                head1 = head1.next;
                tmp = tmp.next;
            } else {
                tmp.next = head2;
                head2 = head2.next;
                tmp = tmp.next;
            }
        }
        if (head1 != null) {
            tmp.next = head1;
        } else {
            tmp.next = head2;
        }
        return newHead.next;
    }

    //从指定位置打印
    public void print2(ListNode head2) {
        ListNode cur = head2;

        while (cur != null) {
            System.out.println(cur.value);
            cur = cur.next;
        }
    }

    /**
     * 21. 合并两个有序链表
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1==null&&list2==null){
            return list1;
        }else if(list1==null){
            return list2;
        }else if(list2==null){
            return list1;
        }else{
            if(list1.value< list2.value){
                ListNode head=list1;
                list1=list1.next;
            }else{
                ListNode head=list2;
                list2=list2.next;
            }
            ListNode cur=head;
            while(list1!=null&&list2!=null){
                if(list1.value> list2.value){
                    cur.next=list2;
                    cur=cur.next;
                    list2=list2.next;
                }else{
                    cur.next=list1;
                    cur=cur.next;
                    list1=list1.next;
                }
            }
            if(list1==null){
                list1.next=list2;
            }else{
                list2.next=list1;
            }
        }
        return head;
    }
}


